<?php
/**
 * Banners_model Class
 * @package TIAP
 * @subpackage Model
 * @category banners
 * @author Suraj Maharjan
 * @link http://twitter.com/
 */
class Packages_model extends MY_Model{
	function Packages_model(){
            parent::MY_Model();
            $this->user_package_relation_table = 'directory_package_relationships';
            $this->package_feature_table = 'package_features';
            $this->package_offer_table = 'package_offers';
	}
    function update_package_status($package_id,$change_val){
         $table = $this->user_package_relation_table;
         $array_update =array('status'=>$change_val);
         $this->db->where('id', $package_id);
         $status = $this->db->update($table, $array_update);
         //echo $this->db->last_query();exit;
         return $status;
    }
    function update_directory_package_list($user_id, $package_id){
        $table = $this->user_package_relation_table;
        $table_offer = $this->package_offer_table;
        $query = $this->db->get_where($table_offer,array('id'=>$package_id));
        $package_offer_detail = $query->row_array();
        $tempArray = explode(" ",$package_offer_detail['offer_name']);
        $time = $tempArray[0];
        if($time<30){
             $mktime = mktime(date('H'), date('i'), date('s'), date('m')+(int)$time, date('d'), date('Y'));
        }else{
            $mktime = mktime(date('H'), date('i'), date('s'), date('m'), date('d')+(int)$time, date('Y'));
        }
        //debug($package_offer_detail);exit;
//        $sqlDelete = "DELETE FROM {$table} WHERE `directory_user_id` = {$user_id}  AND `package_feature_id` = {$package_feature_id}";
//        $this->db->query($sqlDelete);
        $issue_date = date("Y-m-d H:i:s");
        $expire_date = date("Y-m-d H:i:s",$mktime);
        $array_insert = array(
            'directory_user_id'=>$user_id,
            'package_offer_id'=>$package_id,
            'issue_date'=>$issue_date,
            'expire_date'=>$expire_date,
            'status'=>1
        );
        $status = $this->db->insert($table, $array_insert);
        return ($status)?true:false;
    }
    function recharge_directory_package($user_id, $package_id,$prev_expire_date){
        $year = date("Y");
	$month = date("m");
	$day = date("d");
	$hour = date("H");
	$min = date("i");
	$sec = date("s");
        $today = date('Y-m-d H:i:s');
        $diff = abs(strtotime($prev_expire_date) - strtotime($today));

$bonus_years   = floor($diff / (365*60*60*24));
$bonus_months  = floor(($diff - $bonus_years * 365*60*60*24) / (30*60*60*24));
$bonus_days    = floor(($diff - $bonus_years * 365*60*60*24 - $bonus_months*30*60*60*24)/ (60*60*24));
$bonus_hours   = floor(($diff - $bonus_years * 365*60*60*24 - $bonus_months*30*60*60*24 - $bonus_days*60*60*24)/ (60*60));
$bonus_minuts  = floor(($diff - $bonus_years * 365*60*60*24 - $bonus_months*30*60*60*24 - $bonus_days*60*60*24 - $bonus_hours*60*60)/ 60);
$bonus_seconds = floor(($diff - $bonus_years * 365*60*60*24 - $bonus_months*30*60*60*24 - $bonus_days*60*60*24 - $bonus_hours*60*60 - $bonus_minuts*60));

        //$daysDiff = 33;//$prev_expire_date-$today;
        //$bonusDays = (int)$daysDiff;
        $table = $this->user_package_relation_table;
        $table_offer = $this->package_offer_table;
        $query = $this->db->get_where($table_offer,array('id'=>$package_id));
        $package_offer_detail = $query->row_array();
        $tempArray = explode(" ",$package_offer_detail['offer_name']);
        $time = (int)$tempArray[0];
        $mktimeToday = mktime($hour, $min, $sec, $month, $day,$year);
        if($time<30){
             $mktime = mktime($hour+$bonus_hours, $min+$bonus_minuts, $sec+$bonus_seconds, $month+$time+$bonus_months, $day+$bonus_days,$year+$bonus_years);
        }else{
            $mktime = mktime($hour+$bonus_hours, $min+$bonus_minuts, $sec+$bonus_seconds,$month+$bonus_months, $day+$time+$bonus_days, $year+$bonus_years);
        }
        //debug($package_offer_detail);exit;
//        $sqlDelete = "DELETE FROM {$table} WHERE `directory_user_id` = {$user_id}  AND `package_feature_id` = {$package_feature_id}";
//        $this->db->query($sqlDelete);
        $issue_date = date("Y-m-d H:i:s",$mktimeToday);
        $expire_date = date("Y-m-d H:i:s",$mktime);
        $array_insert = array(
            'directory_user_id'=>$user_id,
            'package_offer_id'=>$package_id,
            'issue_date'=>$issue_date,
            'expire_date'=>$expire_date,
            'status'=>1
        );
        $status = $this->db->insert($table, $array_insert);
        return ($status)?true:false;
    }

    function get_package_features($package_type_id){
        $table = $this->package_feature_table;
        $this->db->from($table);
        $this->db->where(array('package_type_id' => $package_type_id ));
        $this->db->order_by('id','asc');

        $query = $this->db->get();
         if ($query->num_rows() > 0) {
        $result = $query->result_array();
            return $result;
        } else {
            return false;
        }

    }
    function get_package_detail_by_user_id($userid=null){
        //echo "__$userid";exit;
        if($userid==null) return false;
        $sql = "SELECT du.email, dc.company,dpr.id,dpr.issue_date,dpr.expire_date,dpr.status,po.offer_name,po.currency_type,po.cost,po.special,pt.name,pt.membership_name,pt.code from directory_user as du ";
        $sql .= "join directory_company as dc on du.id = dc.id ";
        $sql .= "join directory_package_relationships as dpr on du.id = dpr.directory_user_id ";
        $sql .= "join package_offers as po on dpr.package_offer_id=po.id ";
        $sql .= "join package_types as pt on pt.id = po.package_type_id ";
        $sql .= "where du.id = $userid ";
        //$sql .= "group by pt.code ";
        $sql .= "order by dpr.id DESC ";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            return $result;
           return $this->db->last_query();
        } else {
            return false;
        }
    }



}